package com.dg.gaming.client.gui;

import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JLabel;
import javax.swing.*;
import javax.swing.border.EtchedBorder;
import java.awt.Color;
import javax.swing.border.Border;
import java.awt.event.*;
import java.awt.Font;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2009</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class TimerPanel extends JPanel {
    public int seconds;
    public int avgseconds;
    private javax.swing.Timer timer;
    public TimerPanel(JLabel lanel) {
        super();
        try {
            jbInit();
            this.add(lanel, java.awt.BorderLayout.CENTER);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private void jbInit() throws Exception {
        this.setLayout(borderLayout1);
        lblTimer.setFont(new java.awt.Font("Tahoma", Font.BOLD, 11));
        lblTimer.setBorder(border1);
        lblTimer.setToolTipText("");
        lblTimer.setHorizontalAlignment(SwingConstants.CENTER);
        lblTimer.setText("ss");
        this.setOpaque(false);
        this.add(lblTimer, java.awt.BorderLayout.EAST);
        lblTimer.setVisible(false);
    }

    public void startTiming(final int second) {
        this.seconds = second;
        avgseconds = second / 2;
        lblTimer.setVisible(true);
        lblTimer.setText("" + second);
        timer = new javax.swing.Timer(1000, new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                lblTimer.setText("" + seconds);

                seconds--;
                if (seconds <= avgseconds) {
                    lblTimer.setForeground(Color.RED);
                }
                if (seconds < 0) {
                    timer.stop();
                }
            }
        });
        timer.start();
    }

    BorderLayout borderLayout1 = new BorderLayout();
    JLabel lblTimer = new JLabel();
    Border border1 = BorderFactory.createEtchedBorder(EtchedBorder.RAISED,
            Color.white, new Color(148, 145, 140));
}
